package com.example.Lezedata.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatRadioButton;

import com.example.Lezedata.R;

/**
 * copywrite 2015-2020 深圳市万米德网络技术有限公司
 * 不能修改和删除上面的版权声明
 * 此代码属于技术开发组编写，在未经允许的情况下不得传播复制
 * 类描述
 * 2018/5/5 08:52
 * huangshuhui
 */
public class OneMindeRadioButton extends AppCompatRadioButton implements View.OnTouchListener {
    private Paint mPaint;
    private boolean isShowDot;
    private boolean isShowNumberDot;

    private int width;
    private int height;

    /**
     * 顶部图片宽
     */
    private int IntrinsicWidth;

    private String numberText;
    /**
     * 圆点和未读消息的坐标
     */
    private float pivotX;
    private float pivotY;
    /**
     * 圆点半径
     */
    //圆点默认颜色
    private int PAINT_COLOR_DEFAULT = getResources().getColor(R.color.red);
    private final int PAINT_COLOR_WHITE = getResources().getColor(R.color.white);
    int paddinglf, paddingtb, textsize, radius;

    public OneMindeRadioButton(Context context) {
        this(context, null);
    }

    public OneMindeRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OneMindeRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Drawable drawable = getCompoundDrawables()[1];
        if (drawable != null) {
            IntrinsicWidth = drawable.getIntrinsicWidth();
        }

        handler = new Handler();
        setOnTouchListener(this);
        setPadding(dip2px(3), dip2px(3), dip2px(3), dip2px(3));

        paddinglf = getResources().getDimensionPixelOffset(R.dimen.red_dot_paddinglf);
        paddingtb = getResources().getDimensionPixelOffset(R.dimen.red_dot_paddingtb);
        radius = getResources().getDimensionPixelOffset(R.dimen.red_dot_radius);
        textsize = getResources().getDimensionPixelSize(R.dimen.red_dot_textsize);

        mPaint = new Paint();
        mPaint.setStrokeWidth(2);
        mPaint.setAntiAlias(true);
        mPaint.setColor(PAINT_COLOR_DEFAULT);
        mPaint.setTextSize(textsize);
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        super.setCompoundDrawables(left, top, right, bottom);
        Drawable drawable = getCompoundDrawables()[1];
        if (drawable != null) {
            IntrinsicWidth = drawable.getIntrinsicWidth();
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width / 2, 0);
        pivotX = (float) (IntrinsicWidth > width ? IntrinsicWidth / 5 : width / 5);
        pivotY = dip2px(2) + radius;
        if (isShowDot) {
            canvas.drawCircle(pivotX, pivotY, radius, mPaint);
        } else if (isShowNumberDot && !TextUtils.isEmpty(numberText)) {
            float textWidth = mPaint.measureText(numberText);
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));
            pivotY = dip2px(2) + 2 * radius;
            /**
             * 数字左右增加一定的边距
             * */
            mPaint.setColor(PAINT_COLOR_WHITE);
            RectF rectFWhite = new RectF(pivotX - textWidth / 2 - paddinglf, pivotY - 2 * radius, pivotX + textWidth / 2 + paddinglf, pivotY + 2 * radius);
            canvas.drawRoundRect(rectFWhite, 2 * radius, 2 * radius, mPaint);
            mPaint.setColor(PAINT_COLOR_DEFAULT);
            RectF rectF = new RectF(pivotX - textWidth / 2 - paddinglf + dip2px(1), pivotY - 2 * radius + dip2px(1),
                    pivotX + textWidth / 2 + paddinglf - dip2px(1), pivotY + 2 * radius - dip2px(1));
            canvas.drawRoundRect(rectF, 2 * radius, 2 * radius, mPaint);
            mPaint.setColor(PAINT_COLOR_WHITE);
            canvas.drawText(numberText, pivotX - textWidth / 2, pivotY + radius - dip2px(0.5), mPaint);
        }
    }

    /**
     * `
     * 设置是否显示小圆点
     */
    public void setShowSmallDot(boolean isShowDot) {
        this.isShowDot = isShowDot;
        invalidate();
    }


    public void setNumberDot(int text, int color) {
        if (text <= 0) {
            this.isShowNumberDot = false;
            this.numberText = "";
        } else {
            this.isShowNumberDot = true;
            if (text > 99) {
                text = 99;
                this.numberText = text + "+";
            }else{
                this.numberText = text+"";
            }
        }
        if (isShowNumberDot) {
            isShowDot = false;
            PAINT_COLOR_DEFAULT = color;
        }
        invalidate();
    }
    public void setNumberDot(int text) {
        if (text <= 0) {
            this.isShowNumberDot = false;
            this.numberText = "";
        } else {
            this.isShowNumberDot = true;
            if (text > 99) {
                text = 99;
                this.numberText = text + "+";
            }else{
                this.numberText = text+"";
            }
        }
        if (isShowNumberDot) {
            isShowDot = false;
        }
        invalidate();
    }


    public int getUnReadNum() {
        try {
            return Integer.parseInt(numberText);
        } catch (Exception e) {

        }
        return 0;
    }

    public int dip2px(double dip) {
        //dp和px的转换关系比例值
        float density = getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5);
    }

    private Handler handler;
    private static int timeout = 300;//双击间四百毫秒延时
    private int clickCount = 0;//记录连续点击次数

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            clickCount++;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (clickCount == 2 && doubleClickCallBack != null) {
                        doubleClickCallBack.doubleClick();
                    }
                    handler.removeCallbacksAndMessages(null);
                    //清空handler延时，并防内存泄漏
                    clickCount = 0;//计数清零
                }
            }, timeout);//延时timeout后执行run方法中的代码
        }
        return false;//让点击事件继续传播，方便再给View添加其他事件监听
    }

    private DoubleClickCallBack doubleClickCallBack;

    public void setDoubleClickCallBack(DoubleClickCallBack doubleClickCallBack) {
        this.doubleClickCallBack = doubleClickCallBack;
    }

    public interface DoubleClickCallBack {
        public void doubleClick();
    }
}